<?php

/**
  +---------------------------------------<br/>
 * 控制器:微信<br/>
  +---------------------------------------
 * @category membership
 * @package web.front.action
 * @author jason 
 */
class Action_Weixin extends Action {

    public function index() {
        $result = UtilWeixin::token();
        if($result){
            var_dump($result);
        }else{
            echo "ok";
        }
    }

    public function api() {
        $openid = isset($_GET['openid']) ? $_GET['openid'] : 0;
        HttpSession::set("openid", $openid);
        $member = Member::get_one("weixin='" . $openid . "'");
        if ($member) {
            HttpSession::set("cardno", $member->code);
            HttpSession::set("phone", $member->phone);
            $this->redirect("member", "info");
        } else {
            $this->loadCss("resources/css/weixin_api.css");
            $this->loadCss("resources/css/msg.css");
            $this->loadJs("js/msg.js");
            $this->loadJs("js/weixin_api.js");
            $this->view->set("site_name", "绑定会员");
        }
    }

    public function ajaxSave() {
        $mobile = isset($_POST['mobile']) ? $_POST['mobile'] : "";
        $contents = isset($_POST['code']) ? $_POST['code'] : "";
        $openid = HttpSession::get("openid");
        $time = (int)time()-1800;
        $iscode = Code::get_one("sbs_id='" . Gc::$Sbsid . "' and mobile='".$mobile."' and contents='".$contents."' and commitTime>$time");
        if(!$iscode){
            echo "验证码无效";
            exit;
        }
        if ($mobile) {
            $is = Member::get_one("sbs_id='" . Gc::$Sbsid . "' and phone='".$mobile."'");
            if ($is) {
                echo "success";
                exit;
            }
            $api = new UtilApi();
            $result = $api->getMember($mobile);
            if ($result->code == "A00006") {
                $data = $result->data;
                HttpSession::set("cardno", $data->card_no);
                HttpSession::set("phone", $mobile);
                $member = new Member();
                $member->realname = $_POST["realname"];
                $member->password = "";
                $member->phone = $_POST["mobile"];
                $member->code = $data->card_no;
                $member->sbs_id = Gc::$Sbsid;
                $member->weixin = $openid;
                if ($member->save()) {
                    $this->setCoupon($_POST["mobile"]);
                    echo "success";
                    exit;
                } else {
                    echo "绑定失败,请联系管理";
                    exit;
                }
            } else {
                $data = array();
                $data['realname'] = isset($_POST['realname']) ? $_POST['realname'] : "";
                $data['password'] = "123456";
                $data['mobile'] = isset($_POST['mobile']) ? $_POST['mobile'] : "";
                $data['email'] = isset($_POST['email']) ? $_POST['email'] : "";
                $data['gender'] = isset($_POST['gender']) ? $_POST['gender'] : "";
                $tyear = isset($_POST['tyear'])?$_POST['tyear']:"0000";
                $tmon = isset($_POST['tmon'])?$_POST['tmon']:"00";
                $tday = isset($_POST['tday'])?$_POST['tday']:"00";
                $data['birthday'] = $tyear."-".$tmon."-".$tday;
                $result = $api->member_add($data);
                if ($result->code == "A00006") {
                    $member = new Member();
                    $member->realname = $_POST["realname"];
                    $member->password = $carddata[0]->password;
                    $member->phone = $_POST["mobile"];
                    $member->code = $carddata[0]->card_no;
                    $member->sbs_id = Gc::$Sbsid;
                    $member->weixin = $openid;
                    $member->save();
                    HttpSession::set("cardno", $carddata[0]->card_no);
                    HttpSession::set("phone", $data['mobile']);
                    $this->setCoupon($_POST["mobile"]);
                    echo "success";
                    exit;
                }else{
                    echo $result->result;
                    exit;
                } 
            }
        } else {
            echo "请填写手机号";
            exit;
        }
    }

    public function ajaxSend(){
        $mobile = isset($_POST['mobile'])?$_POST['mobile']:"";
        $is = Member::get_one("sbs_id='" . Gc::$Sbsid . "' and phone='".$mobile."'");
        if ($is) {
            $response = array(
                "success" => "no",
                "data" => "此手机号已绑定过",
                "url" => ""
            );
        }else{
            $contents = mt_rand(6,999999);
            $data['mobile']=$mobile;
            $data['contents']="验证码：".$contents." ，验证码在30分钟之内有效";
            $api = new UtilApi();
            $result = $api->send_sms($data);
            if ($result->code == "A00006") {
                $obj = new Code();
                $obj->sbs_id = Gc::$Sbsid;
                $obj->mobile=$mobile;
                $obj->contents=$contents;
                $obj->save();
                $response = array(
                    "success" => "success",
                    "data" => "验证码已发送到手机，请查收",
                    "url" => ""
                );
            }else{
                $response = array(
                    "success" => "no",
                    "data" => $result->msg,
                    "url" => ""
                );
            }  
        }        
        echo json_encode($response);
        exit;
    }

    /**
     *获取优惠券
     */
    public function setCoupon($phone){
        $data['batch_name']  = "锅底抵用券";
        $data['amount']  = 20;
        $data['status']  = 0;
        $data['inv_type'] = 2;
        $data['user_mobile'] = $phone;
        UtilApi::set_coupon($data);
    }

    public function test(){
        $ids = array();
        $member = Member::get();
        if ($member) {
            foreach ($member as $key => $value) {
                if(in_array($value->phone,$ids)){
                    Member::deleteByID($value->member_id);
                }else{
                   $ids[] = $value->phone;
                }
            }
        }
    }

}

?>